﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using tracker.Models;
using tracker.Models.DTO;
using tracker.Models.BLL;
using tracker.Models.DAL;

namespace tracker.Controllers
{
    public class HandlerUpdateUserInfoController : Controller
    {
        //
        // GET: /HandlerUpdateUserInfo/
        [CallAjaxFilter]
        public JsonStringResult Index(
                                    string UserID,
                                    string Permission,
                                    string Name,
                                    string Address,
                                    string CellPhone,
                                    string Email,
                                    string ExpiredTime,
                                    string TelePhone,
                                    string UserLimit,
                                    string AccountStatus,
                                    string UserNote
                                                )
        {
            string result = "";
            if (Utilities.isDateTime(ExpiredTime) && Utilities.isInt(UserLimit)
                )
            {
                //Get update user
                
                EUserLogin user = new EUserLoginBLL().getBySecurityCode(UserID);
                if (user != null)
                {
                    user.Address = Address;
                    user.Email = Email;
                    user.EndDate = DateTime.Parse(ExpiredTime);
                    user.ModifyDate = DateTime.Now;
                    user.Note = UserNote;
                    user.NumberOfDevice = int.Parse(UserLimit);
                    user.Phone = TelePhone;
                    user.Cellphone = CellPhone;
                    user.Fullname = Name;

                    if (Utilities.isInt(AccountStatus))
                    {
                        user.Status = int.Parse(AccountStatus);
                    }
                    else
                    {
                        //Chua bit lam sao
                        //translateStatus(context, ent, accountstatus);
                    }

                    if (Permission == "1" || Permission == "2")
                    {
                        user.UserGroupID = int.Parse(Permission);
                    }
                    else
                    {
                        //Chua bit lam sao
                        //translatePermission(context, ent, userPermission);
                    }

                    String currentUserGroupID = Session[Common.USER_GROUPID].ToString();
                    if (currentUserGroupID == "1" || currentUserGroupID == "2")
                    {
                        if (Session[Common.USER].ToString() == UserID)
                        {
                            //update
                            result = new UserLoginDAL().updateUser(user);
                        }
                        else
                        {
                            //permission
                            result = Common.IS_ERROR;
                        }
                    }
                    else if (currentUserGroupID == "3")
                    {
                        //update
                        result = new UserLoginDAL().updateUser(user);
                    }
                    else if (currentUserGroupID == "4")
                    {
                        //Check is parent user                        
                        string currentUserName = Session[Common.USER].ToString();
                        if (user.ParentCode == currentUserName)
                        {
                            //update
                            result = new UserLoginDAL().updateUser(user);
                        }
                        else
                        {
                            //permission
                            result = Common.IS_ERROR;
                        }
                    }
                }
                else
                {
                    //fail
                    result = Common.IS_ERROR;
                }
            }

            if (result == "0")
            {
                result = "true";
            }
            else
            {
                result = Common.IS_ERROR;
            }
            return new JsonStringResult(result);
        }

    }
}
